<?php

namespace app\wechat\controller\api;

use app\wechat\service\MediaService;
use service\DataService;
use service\WechatService;
use think\Db;
use think\Exception;
use think\facade\Request;

/**
 * 微信接口控制器
 * Class Api
 * @package app\wechat\controller
 * @author Anyon <zoujingli@qq.com>
 */
class Template
{
	function send()
	{
		$config              = [
				'token'          => sysconf('wechat_token'),
				'appid'          => sysconf('wechat_appid'),
				'appsecret'      => sysconf('wechat_appsecret'),
				'encodingaeskey' => sysconf('wechat_encodingaeskey'),
				'mch_id'         => sysconf('wechat_mch_id'),
				'mch_key'        => sysconf('wechat_partnerkey'),
				'ssl_cer'        => sysconf('wechat_cert_cert'),
				'ssl_key'        => sysconf('wechat_cert_key'),
				'cachepath'      => env('cache_path') . 'wechat' . DIRECTORY_SEPARATOR,
		];
		$template            = new \WeChat\Template($config);
		$param               = Request::param();
		$message             = [];
		$message['first']    = empty($param['first']) ? '标题' : $param['first'];
		$message['keyword2'] = empty($param['keyword2']) ? '默认' : $param['keyword2'];
		$message['keyword3'] = empty($param['keyword3']) ? '无' : $param['keyword3'];
		$message['remark']   = empty($param['remark']) ? '' : $param['remark'];
		$message['url']      = empty($param['url']) ? '' : $param['url'];
		$data                = [];
		$data['touser']      = 'otjC01cwz9977szUjUKu8jdQStrA';
		$data['template_id'] = 'Q0nOkrzU1x-hpZ9NaNf6tqsuiumFCdLARShrhBQ2MQQ';
		$data['url']         = $message['url'];
		$data['data']        = ['first' => ['value' => $message['first'], 'color' => '#333333'], 'keyword1' => ['value' => date('Y-m-d H:i:s'), 'color' => ''], 'keyword2' => ['value' => $message['keyword2'], 'color' => '#FF0000'], 'keyword3' => ['value' => $message['keyword3'], 'color' => ''], 'remark' => ['value' => $message['remark'], 'color' => '']];
		$result              = $template->send($data);
		return json($result);
	}
}